﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// 部门岗位DAL
    /// </summary>
    public class HRPosition
    {
        /// <summary>
        /// 获取部门岗位数据
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="deptNo">部门编号</param>
        /// <param name="entityNo">组织机构编号</param>
        /// <returns>部门下岗位DS</returns>
        public DataSet GetListByDeptNo(string deptNo, string entityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  FPositionNo,FDeptNo,FEntityNo,FPositionName,FSeq,FPositionProp from t_HRPosition ");
            strSql.Append(" where FDeptNo=@FDeptNo and FEntityNo=@FEntityNo order by FSeq");
            SqlParameter[] parameters = {
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,15)};

            parameters[0].Value = deptNo;
            parameters[1].Value = entityNo;

            return DbHelperSQL.Query(strSql.ToString(), parameters);

        }

        /// <summary>
        /// 获取所有岗位数据 编号、名称、部门
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="entityNo">组织机构编号</param>
        /// <returns>所有岗位数据列表</returns>
        public DataSet GetListByEntityNo(string entityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  FPositionNo,FDeptNo,FPositionName,FPositionProp from t_HRPosition ");
            strSql.Append(" where FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,15)};

            parameters[0].Value = entityNo;

            return DbHelperSQL.Query(strSql.ToString(), parameters);

        }


        /// <summary>
        /// 增加一条数据
        ///<creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="model">岗位信息</param>
        /// <returns>是否增加成功</returns>
        public bool Add(Model.HRPosition model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_HRPosition(");
            strSql.Append("FPositionNo,FDeptNo,FEntityNo,FPositionName,FSeq,FPositionProp)");
            strSql.Append(" values (");
            strSql.Append("@FPositionNo,@FDeptNo,@FEntityNo,@FPositionName,@FSeq,@FPositionProp)");
            SqlParameter[] parameters = {
					new SqlParameter("@FPositionNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FPositionName", SqlDbType.NVarChar,50),
					new SqlParameter("@FSeq", SqlDbType.Int),
					new SqlParameter("@FPositionProp", SqlDbType.Char,1)};
            parameters[0].Value = model.FPositionNo;
            parameters[1].Value = model.FDeptNo;
            parameters[2].Value = model.FEntityNo;
            parameters[3].Value = model.FPositionName;
            parameters[4].Value = model.FSeq;
            parameters[5].Value = model.FPositionProp;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        ///<creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="model">岗位信息</param>
        /// <returns>是否更新成功</returns>
        public bool Update(Model.HRPosition model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_HRPosition set ");
            strSql.Append("FPositionName=@FPositionName,");
            strSql.Append("FSeq=@FSeq,");
            strSql.Append("FPositionProp=@FPositionProp");
            strSql.Append(" where FPositionNo=@FPositionNo and FDeptNo=@FDeptNo and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FPositionName", SqlDbType.NVarChar,50),
					new SqlParameter("@FSeq", SqlDbType.Int),
					new SqlParameter("@FPositionProp", SqlDbType.Char,1),
					new SqlParameter("@FPositionNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
            parameters[0].Value = model.FPositionName;
            parameters[1].Value = model.FSeq;
            parameters[2].Value = model.FPositionProp;
            parameters[3].Value = model.FPositionNo;
            parameters[4].Value = model.FDeptNo;
            parameters[5].Value = model.FEntityNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        ///<creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="fPositionNo">岗位编号</param>
        /// <param name="fDeptNo">部门编号</param>
        /// <param name="fEntityNo">组织机构编号</param>
        /// <returns>是否删除成功</returns>
        public bool Delete(string FPositionNo, string FDeptNo, string FEntityNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_HRPosition ");
            strSql.Append(" where FPositionNo=@FPositionNo and FDeptNo=@FDeptNo and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FPositionNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)			};
            parameters[0].Value = FPositionNo;
            parameters[1].Value = FDeptNo;
            parameters[2].Value = FEntityNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        /// <param name="fPositionNo">岗位编号</param>
        /// <param name="fDeptNo">部门编号</param>
        /// <param name="fEntityNo">组织机构编号</param>
        /// <returns>是否存在该记录</returns>
        public bool Exists(string FPositionNo, string FDeptNo, string FEntityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_HRPosition");
            strSql.Append(" where FPositionNo=@FPositionNo and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FPositionNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)			};
            parameters[0].Value = FPositionNo;
            parameters[1].Value = FEntityNo;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 获取部门正级的岗位编号
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="deptNo">部门编号</param>
        /// <param name="entityNo">组织机构代码</param>
        /// <param name="prop">性质</param>
        /// <returns>正级岗位编号</returns>
        public string GetByPositionProp(string deptNo, string entityNo, string prop)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 FPositionNo from  t_HRPosition");
            strSql.Append(" where FDeptNo=@FDeptNo and FEntityNo=@FEntityNo and FPositionProp=@FPositionProp");

            SqlParameter[] parameters = {
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,15),
                    new SqlParameter("@FPositionProp", SqlDbType.Char,1)};

            parameters[0].Value = deptNo;
            parameters[1].Value = entityNo;
            parameters[2].Value = prop;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (null != obj)
            {
                return obj.ToString();
            }
            else
            {
                return string.Empty;
            }

        }


        /// <summary>
        /// 得到一个实体对象
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="fPositionNo">岗位编号</param>
        /// <param name="fDeptNo">部门编号</param>
        /// <param name="fEntityNo">组织机构编号</param>
        /// <returns>岗位信息</returns>
        public Model.HRPosition GetModel(string FPositionNo, string FDeptNo, string FEntityNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FPositionNo,FDeptNo,FEntityNo,FPositionName,FSeq,FPositionProp from t_HRPosition ");
            strSql.Append(" where FPositionNo=@FPositionNo and FDeptNo=@FDeptNo and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FPositionNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)			};
            parameters[0].Value = FPositionNo;
            parameters[1].Value = FDeptNo;
            parameters[2].Value = FEntityNo;

            Model.HRPosition model = new Model.HRPosition();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["FPositionNo"] != null && ds.Tables[0].Rows[0]["FPositionNo"].ToString() != "")
                {
                    model.FPositionNo = ds.Tables[0].Rows[0]["FPositionNo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FDeptNo"] != null && ds.Tables[0].Rows[0]["FDeptNo"].ToString() != "")
                {
                    model.FDeptNo = ds.Tables[0].Rows[0]["FDeptNo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FEntityNo"] != null && ds.Tables[0].Rows[0]["FEntityNo"].ToString() != "")
                {
                    model.FEntityNo = ds.Tables[0].Rows[0]["FEntityNo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FPositionName"] != null && ds.Tables[0].Rows[0]["FPositionName"].ToString() != "")
                {
                    model.FPositionName = ds.Tables[0].Rows[0]["FPositionName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FSeq"] != null && ds.Tables[0].Rows[0]["FSeq"].ToString() != "")
                {
                    model.FSeq = int.Parse(ds.Tables[0].Rows[0]["FSeq"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FPositionProp"] != null && ds.Tables[0].Rows[0]["FPositionProp"].ToString() != "")
                {
                    model.FPositionProp = ds.Tables[0].Rows[0]["FPositionProp"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }
    }
}
